class Polynomial(object):

    # def __init__(self):
    #     pass
        # self._coefficient = None
        # self._exponential = None
    # 不在__init__中进行初始化，而是借助classmethod进行赋值操作
    
    @property
    def coefficient(self):
        return self._coefficient
    
    @property
    def exponential(self):
        return self._exponential
    
    @coefficient.setter
    def coefficient(self, coefficient):
        self._coefficient = coefficient
    
    @exponential.setter
    def exponential(self, exponential):
        print("Setter")
        self._exponential = exponential

    @classmethod
    def preprocess(cls, ipt):
        """_summary_

        Args:
            ipt (str): 输入一个字符串
            e.g. "1,2,3,4,5,5",
            用逗号分开，最后一个一定是整数（表示多项式项数）
            前面的表示系数（从常数项开始）
            上述字符串表示：
            y = 1 + 2x + 3x^2 + 4x^3 + 5x^4

        Returns:
            _type_: _description_

            
        """
        array = ipt.split(",")
        res = cls()
        print("Classmethod")
        res.coefficient = [float(array[i]) for i in range(len(array) - 1)]
        res.exponential = int(array[-1])
        return res

    def __add__(self, b):
        new_exp = max(b.exponential, self.exponential)
        new_coe = [0 for _ in range(new_exp)]
        for i in range(new_exp):
            if i < b.exponential:
                new_coe[i] += b.coefficient[i]
            if i < self.exponential:
                new_coe[i] += self.coefficient[i]
        result = ",".join([str(round(i, 2)) for i in new_coe])
        result += ("," + str(new_exp) )
        return Polynomial.preprocess(result)


    def __sub__(self, b):
        new_exp = max(b.exponential, self.exponential)
        new_coe = [0 for _ in range(new_exp)]
        for i in range(new_exp):
            if i < b.exponential:
                new_coe[i] -= b.coefficient[i]
            if i < self.exponential:
                new_coe[i] += self.coefficient[i]
        
        result = ",".join([str(round(i, 2)) for i in new_coe])
        result += ("," + str(new_exp) )
        return Polynomial.preprocess(result)
        


    def __mul__(self, b):
        new_coe = [0 for _ in range(b.exponential + self.exponential)]
        new_exp = b.exponential + self.exponential
        for i in range(self.exponential):
            for j in range(b.exponential):
                new_coe[i + j] += (self.coefficient[i] *  b.coefficient[j])

        result = ",".join([str(round(i, 2)) for i in new_coe])
        result += ("," + str(new_exp) )
        return Polynomial.preprocess(result)

    def __repr__(self):
        res = "Polynomial: "
        for i in range(self.exponential):
            if self.coefficient[i] > 0 and i > 0:
                res += f"+{round(self.coefficient[i], 2)}x^{i}"
            elif self.coefficient[i] < 0:
                res += f"{round(self.coefficient[i], 2)}x^{i}" 
            elif i == 0 and self.coefficient[i] != 0:
                res += f"{round(self.coefficient[i], 2)}"   
        return res

if __name__ == "__main__":
    A = Polynomial.preprocess("1,-2.3,3,3")
    B = Polynomial.preprocess("0,0,4,5,6,5")
    print(A)
    print(B)
    C = A * B
    print(C)


